/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import Beans.ModSeg_Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Jorge Aguilar
 */
public class validador extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String accion = request.getParameter("opcion");
        if(accion == null)
        {
        } else if(accion.equals("LOGIN"))
        {   String user = request.getParameter("txtUsuario");
            String pass = request.getParameter("txtClave");
            ModSeg_Usuario u = new ModSeg_Usuario();
            ModSeg_Usuario uobt = new ModSeg_Usuario();
            uobt = u.validarUsuario(user, pass);
            if(uobt != null)
            {   if(uobt.getUsuario() != null)
                {   if (uobt.getPassword() != null)
                    {   HttpSession sesion = request.getSession(false);
                        if(sesion != null)
                        {   sesion.invalidate();    }
                        sesion = request.getSession();
                        sesion.setAttribute("ID", sesion.getId());
                        sesion.setAttribute("user", uobt.getUsuario());
                        sesion.setAttribute("iduser", uobt.getIdusuario());
                        sesion.setAttribute("idempleado", uobt.getFk_idempleado());
                        response.sendRedirect("home.jsp");
                    }                    
                } else 
                {   response.sendRedirect("index.jsp?msg= Usuario no registrado o clave incorrecta. Por favor verifique");  }
            } else
            {   response.sendRedirect("index.jsp?msg= Usuario no registrado o clave incorrecta. Por favor verifique");
            }
        } else if(accion.equals("LOGOUT"))
        {   HttpSession session = request.getSession(false);
            session.removeAttribute("id");
            session.removeAttribute("iduser");
            session.removeAttribute("user");
            session.invalidate();
            response.sendRedirect("index.jsp");
        } else if(accion.equals("ENVIAR"))
        {   response.sendRedirect("lockscreen.jsp");
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
